<?php
session_start();
include 'dbconfig.php'; 

mysql_select_db($db, $con);
if (isset($_SESSION['uid'])) {
	$uid = $_SESSION['uid'];

	$c_str = explode("@", $_POST["courses"]);
	$m_str = explode("@", $_POST["modules"]);


	$courses = Array();
	$exams = Array();

	foreach ($c_str as $c) {
		if (strlen($c)>0) {
			$cdata = explode("|", $c);
			$course = Array($uid, $cdata[0], floatval($cdata[1]), $cdata[2]);
			array_push($courses, $course);	
		}
	}
	foreach ($m_str as $m) {
		if (strlen($m)>0) {
			$mdata = explode("|", $m);
			$exam = Array($uid, $mdata[0], $mdata[1], floatval($mdata[2]));
			array_push($exams, $exam);	
		}
	}

	// First delete everything
	$query = "DELETE FROM courses WHERE uid = '".$uid."'";
	mysql_query($query);
	$query = "DELETE FROM exams WHERE uid = '".$uid."'";
	mysql_query($query);

	// Then insert new data.

	foreach ($courses as $course){
		$query = "INSERT INTO courses VALUES (0,'".$course[0]."','".$course[1]."',".$course[2].",'".$course[3]."')";
		mysql_query($query);
	}

	foreach ($exams as $exam){
		$query = "INSERT INTO exams VALUES (0,'".$exam[0]."','".$exam[1]."',".$exam[2].",".$exam[3].")";
		mysql_query($query);
	}

	mysql_close($con);

	echo "ok";
} else {
	echo "login";
}
?>
